package com.onegene.pdf.component.report;

import com.itextpdf.html2pdf.attach.impl.tags.ImgTagWorker;
import com.itextpdf.layout.Style;
import com.itextpdf.layout.element.Div;
import com.itextpdf.layout.element.IElement;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Text;
import com.itextpdf.layout.property.LineHeight;
import com.itextpdf.layout.property.Property;
import com.itextpdf.layout.property.TransparentColor;
import com.onegene.pdf.component.entity.ParaExtraParam;
import com.onegene.pdf.component.entity.ReportBean;
import com.onegene.pdf.entity.SampleResult;

import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author: laoliangliang
 * @description:
 * @create: 2020/7/6 16:52
 **/
public class CommonHandler {
    public static java.util.List<ReportBean.ItemsBean.ContentsBean> handlerContents(ReportBean.ItemsBean itemsBean) {
        java.util.List<ReportBean.ItemsBean.ContentsBean> contents = itemsBean.getContents();
        int size = contents.size();
        if (size > 6) {
            java.util.List<ReportBean.ItemsBean.ContentsBean> result = new ArrayList<>();
            java.util.List<ReportBean.ItemsBean.ContentsBean> contentsBeans = contents.subList(2, 6);
            result.add(contents.get(0));
            result.add(contents.get(1));
            for (int i = 6; i < size; i++) {
                ReportBean.ItemsBean.ContentsBean contentsBean = contents.get(i);
                contentsBean.setLabel(contentsBean.getLabel().split("-")[0]);
                result.add(contentsBean);
            }
            itemsBean.setContents(result);
            result.add(contentsBeans.get(contentsBeans.size() - 1));
        } else {
            for (ReportBean.ItemsBean.ContentsBean content : contents) {
                if (content.getLabel().contains("线下")) {
                    content.setLabel(content.getLabel().split("-")[0]);
                }
            }
        }
        return itemsBean.getContents();
    }

    public static void addParagraphStyleCircle(Style style, java.util.List<IElement> children) {
        addParagraphStyleCircle(style, children, new Div());
    }

    public static void addParagraphStyleCircle(Style style, java.util.List<IElement> children, Div pDiv) {
        addParagraphStyleCircle(style, children, pDiv, new ParaExtraParam());
    }

    public static void addParagraphStyleCircle(Style style, java.util.List<IElement> children, Div pDiv, ParaExtraParam paraExtraParam) {
        for (IElement child : children) {
            if (child instanceof Paragraph) {
                Paragraph element = (Paragraph) child;
                Style styleTemp = new Style();
                styleTemp.setProperty(Property.TEXT_ALIGNMENT, pDiv.getProperty(Property.TEXT_ALIGNMENT));
                if (paraExtraParam.getLineHeightEnabled()) {
                    child.setProperty(Property.LINE_HEIGHT, LineHeight.createMultipliedValue(paraExtraParam.getLineHeight()));
                }
                if (paraExtraParam.getFirstIndent()) {
                    element.setFirstLineIndent(paraExtraParam.getIndent());
                }
                element.addStyle(style);
                element.addStyle(styleTemp);
                java.util.List<IElement> children1 = element.getChildren();
                addParagraphStyleCircle(style, children1, pDiv, paraExtraParam);
            }
            if (child instanceof Div) {
                Div div = (Div) child;
                java.util.List<IElement> children1 = div.getChildren();
                addParagraphStyleCircle(style, children1, (Div) child, paraExtraParam);
            }
            if (child instanceof Text) {
                child.setProperty(Property.FONT_SIZE, style.getProperty(Property.FONT_SIZE));
                Text text = (Text) child;
                text.addStyle(style);
            }
        }
    }

    private static AtomicInteger cutCount = new AtomicInteger(0);

    public static void addParagraphStyleCircle(java.util.List<IElement> children, Paragraph paragraph) {
        for (IElement child : children) {
            if (child instanceof Paragraph) {
                Paragraph element = (Paragraph) child;
                java.util.List<IElement> children1 = element.getChildren();
                addParagraphStyleCircle(children1, paragraph);
                if (cutCount.get() == 0) {
                    paragraph.add(new Text("\n"));
                } else {
                    cutCount.decrementAndGet();
                }
            }
            if (child instanceof Div) {
                Div div = (Div) child;
                java.util.List<IElement> children1 = div.getChildren();
                addParagraphStyleCircle(children1, paragraph);
            }
            if (child instanceof ImgTagWorker) {
                ImgTagWorker img = (ImgTagWorker) child;
            }
            if (child instanceof Text) {
                String text1 = ((Text) child).getText();
//                if ("\n".equals(text1)) {
//                    cutCount.incrementAndGet();
//                    continue;
//                }
                if (text1.startsWith("      ")) {
                    if (text1.startsWith("      检测结果")) {
                        text1 = text1.replace("      ", "        ");
                    } else {
                        text1 = text1.replace("      ", "       ");
                    }
                }
                Text element = new Text(text1);

                // 处理特殊字体，比如字体颜色
                Style style = handlerChildStyle(child);
                element.addStyle(style);
                paragraph.add(element);
            }
        }
    }

    public static void handlerElement(IElement iElement, Paragraph paragraph) {
        if (iElement instanceof Div) {
            Div div = (Div) iElement;
            java.util.List<IElement> children = div.getChildren();
            // 全部段落改成相同样式
            CommonHandler.addParagraphStyleCircle(children, paragraph);
        } else if (iElement instanceof Paragraph) {
            java.util.List<IElement> children = new ArrayList<>();
            children.add(iElement);
            CommonHandler.addParagraphStyleCircle(children, paragraph);
        }
    }

    private static Style handlerChildStyle(IElement child) {
        Object fontColor = child.getProperty(Property.FONT_COLOR);
//        Object fontWight = child.getProperty(Property.FONT_WEIGHT);
        Style style = new Style();
        if (fontColor != null) {
            TransparentColor color = (TransparentColor) fontColor;
            style.setFontColor((color.getColor()));
        }
//        if ("bold".equals(fontWight)) {
//            style.setBold();
//        }
        return style;
    }

    public static void sortGaoLevel(java.util.List<ReportBean.ItemsBean> gaoLevel) {
        gaoLevel.sort((s1, s2) -> {
            if (s1.getIndex() != s2.getIndex()) {
                return s2.getIndex() - s1.getIndex();
            } else {
                if (s1.getCategoryRank().equals(s2.getCategoryRank())) {
                    return s1.getRank() - s2.getRank();
                } else {
                    return s1.getCategoryRank() - s2.getCategoryRank();
                }
            }
        });
    }

    public static Integer setPdfVersion(SampleResult sampleResult, Integer version, String profile) {
        if ("pro".equals(profile) || "local".equals(profile)) {
            if ("5e5ef8bac96786104d18d53b".equals(sampleResult.getTemplateRefId())) {
                if ("PI397661627015827456".equals(sampleResult.getPackageCode()) || "PI397923900003258368".equals(sampleResult.getPackageCode())) {
                    version = 3;
                } else if ("PI397539133961359360".equals(sampleResult.getPackageCode())) {
                    version = 4;
                }
            } else if ("5e5ef937c96786104d18d555".equals(sampleResult.getTemplateRefId())) {
                if ("PI397538973256601600".equals(sampleResult.getPackageCode())) {
                    version = 2;
                } else if ("PI397538867614666752".equals(sampleResult.getPackageCode())) {
                    version = 1;
                }
            }
        } else {
            if ("5e5ef8bac96786104d18d53b".equals(sampleResult.getTemplateRefId())) {
                if ("PI397661627015827456".equals(sampleResult.getPackageCode()) || "PI397923900003258368".equals(sampleResult.getPackageCode())) {
                    version = 3;
                } else if ("PI397539133961359360".equals(sampleResult.getPackageCode())) {
                    version = 4;
                }
            } else if ("5e5ef937c96786104d18d555".equals(sampleResult.getTemplateRefId())) {
                if ("PI397538973256601600".equals(sampleResult.getPackageCode())) {
                    version = 2;
                } else if ("PI397538867614666752".equals(sampleResult.getPackageCode())) {
                    version = 1;
                }
            }
        }
        return version;
    }
}
